/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.sample.library.service.impl;

import java.util.List;

import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
import com.liferay.portal.kernel.dao.search.SearchContainer;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.sample.library.model.Category;
import com.sample.library.portlet.search.category.CategorySearchTerms;
import com.sample.library.service.CategoryLocalServiceUtil;
import com.sample.library.service.base.CategoryLocalServiceBaseImpl;

/**
 * The implementation of the category local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link com.sample.library.service.CategoryLocalService} interface.
 *
 * <p>
 * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
 * </p>
 *
 * @author Nguyen Tuan Anh
 * @see com.sample.library.service.base.CategoryLocalServiceBaseImpl
 * @see com.sample.library.service.CategoryLocalServiceUtil
 */
public class CategoryLocalServiceImpl extends CategoryLocalServiceBaseImpl {
	/*
	 * NOTE FOR DEVELOPERS:
	 *
	 * Never reference this interface directly. Always use {@link com.sample.library.service.CategoryLocalServiceUtil} to access the category local service.
	 */
	
	@SuppressWarnings("unchecked")
	public List<Category> search(SearchContainer searchContainer, int start, int end,
			String orderByType, OrderByComparator comparator) {
		CategorySearchTerms searchTerms = (CategorySearchTerms)searchContainer.getSearchTerms();
		DynamicQuery dq = (DynamicQuery) DynamicQueryFactoryUtil.forClass(Category.class);
		
		if(searchTerms.getName() != null && !searchTerms.getName().equals(""))	{
			dq.add(PropertyFactoryUtil.forName("name").like("%"+searchTerms.getName()+"%"));
		}
		if (orderByType.equalsIgnoreCase("asc"))
		{
			dq.addOrder(PropertyFactoryUtil.forName(comparator.getOrderBy()).asc());
		}
		else
		{
			dq.addOrder(PropertyFactoryUtil.forName(comparator.getOrderBy()).desc());
		}
		List<Category> list = null;
		try {
			list = (List<Category>)CategoryLocalServiceUtil.dynamicQuery(dq);
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	public int searchCount(SearchContainer searchContainer) {
		CategorySearchTerms searchTerms = (CategorySearchTerms)searchContainer.getSearchTerms();
		DynamicQuery dq = (DynamicQuery) DynamicQueryFactoryUtil.forClass(Category.class);
		
		if(searchTerms.getName() != null && !searchTerms.getName().equals(""))	{
			dq.add(PropertyFactoryUtil.forName("name").like("%"+searchTerms.getName()+"%"));
		}
		long count = 0;
		try {
			count = CategoryLocalServiceUtil.dynamicQueryCount(dq);
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return (int)count;
	}

	
}